package com.zonski.jbots.engine.collision;

import com.zonski.jbots.engine.CollisionEffect;
import com.zonski.jbots.engine.CollisionData;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Directions;
import com.zonski.jbots.engine.Rectangle;

public class SlideCollisionEffect implements CollisionEffect
{
    public static final SlideCollisionEffect INSTANCE = new SlideCollisionEffect();

    private SlideCollisionEffect()
    {
    }
    
    public boolean apply(Entity entity, Engine engine, CollisionData collision)
    {
        int x = entity.xVelocity;
        int y = entity.yVelocity;
        int turn = entity.turnVelocity;
        int steps = entity.stepsRemaining;
        Rectangle bounds = entity.bounds;
        // stop moving in the collision direction
        switch(collision.edge)
        {
            case Directions.EAST:
                x = 0;
                bounds.setX(collision.x - bounds.width);
                break;
            case Directions.WEST:
                x = 0;
                bounds.setX(collision.x);
                break;
            case Directions.SOUTH:
                y = 0;
                bounds.setY(collision.y - bounds.height);
                break;
            case Directions.NORTH:
                y = 0;
                bounds.setY(collision.y);
                break;
        }
        entity.setVelocity(x, y, turn, steps - collision.cost);
        return true;
    }

    public final int getCollisionType(Entity entity, Entity with)
    {
        return DETAILED_COLLISIONS;
    }
}
